package com.example.test.多线程;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author 晓果冻
 * @version 1.0
 * @date 2022/5/27 15:25
 */
public class CallableDemo implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println(Thread.currentThread().getName()+"我是Runnable子线程");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"Callable子线程阻塞完毕");
        return "我是Callable";
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CallableDemo callableDemo = new CallableDemo();
        FutureTask<String> futureTask = new FutureTask<>(callableDemo);
        new Thread((futureTask)).start();
        String result = futureTask.get();
        System.out.println(result);
        System.out.println("主线程执行完毕");
    }
}
